package com.example.mapstruct.utils;

import com.example.mapstruct.pojo.dto.CarDTO;
import com.example.mapstruct.pojo.dto.DriverDTO;
import com.example.mapstruct.pojo.dto.PartDTO;
import com.example.mapstruct.pojo.vo.CarVO;
import com.example.mapstruct.pojo.vo.DriverVO;
import com.example.mapstruct.pojo.vo.VehicleVO;
import org.mapstruct.*;
import org.springframework.util.CollectionUtils;
import java.util.List;

/**
 * @ClassName CarConvert
 * @Description
 * @Author gqz
 * @Date 2022/2/9 10:38
 * @Version 1.0
 **/
@Mapper(componentModel = "spring")
public abstract class   CarConvert {

//   public static CarConvert INSTANCE= Mappers.getMapper(CarConvert.class);

    @Mappings(
            {
                    @Mapping(source = "totalPrice",target = "totalPrice",numberFormat = "#.00"),
                    @Mapping(source = "publishDate",target = "publishDate",dateFormat = "yyyy-MM-dd HH:mm:ss"),
                    @Mapping(source = "brand",target = "brandName"),
                    @Mapping(target = "color",ignore = true),
                    @Mapping(source = "driverDTO",target = "driverVO")
            }
    )
    public abstract CarVO carDtoToCarVo(CarDTO carDTO);

    @Mappings({
            @Mapping(source = "id",target = "driverId"),
            @Mapping(source = "name",target = "fullName")
    })
    public abstract DriverVO driverDTOToDriverVO(DriverDTO driverDTO);

    @AfterMapping //表示让mapstruct在调用自动转换的方法之后,会来自动调用本方法
    public void afterCarDtoToCarVo(CarDTO carDTO,@MappingTarget CarVO carVO){
        //@MappingTarget:表示传来的carVo对象是已经赋值过的
        List<PartDTO> partDTOs = carDTO.getPartDTOs();
        boolean flag = !CollectionUtils.isEmpty(partDTOs);
        carVO.setHasPart(flag);
    }

    public abstract List<CarVO> batchCarDTOTOCarVO(List<CarDTO> carDTOList);

    /**
     * @BeanMapping:配置忽略mapStruct的默认映射行为,只映射那些配置了@Mapping的属性
     * @param carDTO
     * @return
     */
    @BeanMapping(ignoreByDefault = true )
    @Mapping(source = "id",target = "id")
    @Mapping(source = "brand",target = "brandName")
    public abstract VehicleVO carDTOTOVehicleVO(CarDTO carDTO);

    @InheritConfiguration
    @Mapping(target = "id",ignore = true)
    public abstract void updateVehicleVo(CarDTO carDTO,@MappingTarget VehicleVO vehicleVO);

    @InheritInverseConfiguration(name = "carDTOTOVehicleVO")
    public abstract CarDTO vehicleVOTOCarDTO(VehicleVO vehicleVO);
}
